package com.xlhj.boot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: caijliu
 * @Date: 02/09/2021 16:45
 * @Description TODO
 * @Vsersion: 0.0.1
 */
@Configuration
public class RabbitFanoutConfig {

    private static final String FANOUT_EXCHANGE_NAME = "fanout-exchange";
    private static final String FANOUT_QUEUE_NAME_A = "fanout-queue-A";
    private static final String FANOUT_QUEUE_NAME_B = "fanout-queue-B";
    private static final String FANOUT_QUEUE_NAME_C = "fanout-queue-C";

    /**
     * 声明Exchange
     * @return
     */
    @Bean(name = "fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE_NAME);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "fanoutQueueA")
    public Queue fanoutQueueA() {
        return QueueBuilder.durable(FANOUT_QUEUE_NAME_A).build();
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "fanoutQueueB")
    public Queue fanoutQueueB() {
        return QueueBuilder.durable(FANOUT_QUEUE_NAME_B).build();
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "fanoutQueueC")
    public Queue fanoutQueueC() {
        return QueueBuilder.durable(FANOUT_QUEUE_NAME_C).build();
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding queueBindingA(@Qualifier("fanoutQueueA") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding queueBindingB(@Qualifier("fanoutQueueB") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding queueBindingC(@Qualifier("fanoutQueueC") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }
}
